package com.novel.controller.backend;

import com.novel.core.common.req.AdminPageReqDto;
import com.novel.core.common.resp.PageRespDto;
import com.novel.core.common.resp.RestResp;
import com.novel.core.constant.ApiRouterConsts;
import com.novel.core.constant.SystemConfigConsts;
import com.novel.dto.req.AdminLoginReqDto;
import com.novel.dto.req.AuthorInfoReqDto;
import com.novel.dto.req.BookListRepDto;
import com.novel.dto.req.UserInfoReqDto;
import com.novel.dto.resp.*;
import com.novel.entity.Admin;
import com.novel.service.AdminService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wang
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@SecurityRequirement(name = SystemConfigConsts.HTTP_AUTH_HEADER_NAME)
@RequestMapping(ApiRouterConsts.API_ADMIN_URL_PREFIX)
//@Transactional
public class AdminController {

    private final AdminService adminService;

    /**
     * 管理员登录
     */
    @PostMapping("login")
    public RestResp<AdminLoginRespDto> login(@Valid @RequestBody AdminLoginReqDto dto) {
        return adminService.login(dto);
    }

    /**
     * 查询 书籍分类信息接口
     */
    @GetMapping("bookCategory")
    public RestResp<List<BookCategoryRespDto>> getBookCategory() {
        return adminService.getBookCategory();
    }

    /**
     * 查询 管理员列表
     */
    @GetMapping("admin/list")
    public RestResp<PageRespDto<AdminInfoRespDto>> listAdmin(AdminPageReqDto dto) {
        return adminService.getAdminInfo(dto);
    }

    /**
     * 添加管理员
     */
    @PostMapping("admin/add")
    public RestResp<Void> addAdmin(@Valid @RequestBody Admin admin) {
        return adminService.addAdmin(admin);
    }

    /**
     * 根据ID查询管理员信息
     */
    @GetMapping("admin/showAdminById/{id}")
    public RestResp<Admin> showAdminById(@PathVariable("id") Long id) {
        return adminService.showAdminById(id);
    }

    /**
    * 更新管理员信息
    */
    @PutMapping("admin/update")
    public RestResp<Void> updateAdmin(@Valid @RequestBody Admin admin) {
        return adminService.updateAdmin(admin);
    }

    /**
     * 删除管理员
     */
    @DeleteMapping("admin/delete/{id}")
    public RestResp<Void> deleteAdmin(@PathVariable("id") Long id) {
        return adminService.deleteAdminById(id);
    }

    /**
     * 查询作者信息
     */
    @GetMapping("author/list")
    public RestResp<PageRespDto<AuthorInfoRespDto>> getAuthorList(AuthorInfoReqDto authorInfoReqDto) {
        return adminService.getAuthorList(authorInfoReqDto);
    }

    /**
     * 根据id删除作者
     */
    @DeleteMapping("author/delete/{id}")
    public RestResp<Void> deleteAuthorById(@PathVariable("id") Long id) {
        return adminService.deleteAuthorById(id);
    }

    /**
     * 查询用户信息
     */
    @GetMapping("user/list")
    public RestResp<PageRespDto<UserInfoListRespDto>> getUserList(UserInfoReqDto userInfoReqDto) {
        return adminService.getUserList(userInfoReqDto);
    }

    /**
     * 根据id删除用户
     */
    @DeleteMapping("user/delete/{id}")
    public RestResp<Void> deleteUserById(@PathVariable("id") Long id) {
        return adminService.deleteUserById(id);
    }

    @GetMapping("book/list")
    public RestResp<PageRespDto<BookListRespDto>> getBookList(BookListRepDto bookListReqDto) {

        return adminService.getBookList(bookListReqDto);
    }

    /**
     * 根据id删除小说
     */
    @DeleteMapping("book/delete/{id}")
    public RestResp<Void> deleteBookById(@PathVariable("id") Long id) {
        return adminService.deleteBookById(id);
    }
}
